package v1

import (
	"fmt"
	"ginblog/model"
	"ginblog/utils/errmsg"
	_ "github.com/deckarep/golang-set"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 添加文章
func AddArticle(c *gin.Context){
	// 引入结构体
	var data model.Article
	_ = c.ShouldBindJSON(&data)
	code = model.CreateArt(&data)

	c.JSON(http.StatusOK, gin.H{
		"status":code,
		"data":data,
		"message":errmsg.GetErrorMsg(code),
	})

}

//  查询分类下所有文章
func GetCateArt(c *gin.Context){
	pageSize,_ := strconv.Atoi(c.Query("pagesize"))
	pageNum,_ := strconv.Atoi(c.Query("pagenum"))
	id,_ := strconv.Atoi(c.Param("id"))

	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = -1
	}

	data,code,total := model.GetCateArt(id,pageSize,pageNum)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":data,
		"total":total,
		"message":errmsg.GetErrorMsg(code),
	})
}

// 查询单个文章
func GetArtInfo(c *gin.Context)  {
	uid,_ := c.Get("userid")
	id,_ := strconv.Atoi(c.Param("id")) // 转为int类型
	var viewData model.View
	viewData.Uid = uid.(int)
	viewData.Aid = id
	data,code := model.GetArtInfo(id) // 单个文章详情
	pageSize,_ := strconv.Atoi(c.DefaultQuery("pagesize", "10"))
	pageNum,_ := strconv.Atoi(c.DefaultQuery("pagenum", "1"))
	// 查询这个文章的点赞列表
	praiseList,code := model.GetArtPraiseList(id,pageSize,pageNum)
	var praiseUser []model.User // 定义一个空切片，组装点赞的用户数据
	if code == errmsg.SUCCSE{
		for _, v := range praiseList {
			praiseUser = append(praiseUser,v.User)
		}
	}

	// 查询是否浏览过
	viewCode :=model.IsViewArt(uid.(int),id)

	if viewCode == errmsg.ERROR{
		model.CreateView(&viewData)
	}else{
		model.SaveView(&viewData)
	}

	model.IncrViewArt(id) // 浏览数+1

	// 查询用户所有的文章点赞记录
	uidList,_ := model.GetUserArtId(uid.(int)) // 经典的类型断言
	_,find :=model.FindPraise(uidList,id)
	if find {
		data.Ispraise = 1 // 判断用户是否点赞过1点赞，0没有点赞
	}else{
		data.Ispraise = 0
	}

	// 特别重要的一种定义数据的类型
	var maps = make(map[string]interface{})
	maps["article"] = data
	maps["praise_list"] = praiseUser

	fmt.Printf("v ==== %+v \n", praiseList)

	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":maps,
		"message":errmsg.GetErrorMsg(code),
	})
}

//  查询文章列表
func GetArt(c *gin.Context){
	uid,_ := c.Get("userid")
	pageSize,_ := strconv.Atoi(c.DefaultQuery("pagesize", "10"))
	pageNum,_ := strconv.Atoi(c.DefaultQuery("pagenum", "1"))
	data,code,total := model.GetArt(pageSize, pageNum)

	// 查询用户所有的文章点赞记录
	uidList,_ := model.GetUserArtId(uid.(int)) // 经典的类型断言
	var articleList []model.Article
	for _, v := range data {
		 _,find :=model.FindPraise(uidList,int(v.ID))
		if find {
			v.Ispraise = 1 // 判断用户是否点赞过1点赞，0没有点赞
		}else{
			v.Ispraise = 0
		}
		articleList = append(articleList,v)
	}
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"data":articleList,
		"total":total,
		"message":errmsg.GetErrorMsg(code),
	})
}


// 编辑文章
func EditArt(c *gin.Context){
	var data model.Article
	id,_ := strconv.Atoi(c.Param("id"))
	c.ShouldBindJSON(&data)

	code = model.EditArt(id, &data)

	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"message":errmsg.GetErrorMsg(code),
	})
}


// 删除文章
func DeleteArt(c *gin.Context){
	id,_ := strconv.Atoi(c.Param("id"))

	code = model.DeleteArt(id)
	c.JSON(http.StatusOK,gin.H{
		"status":code,
		"message":errmsg.GetErrorMsg(code),
	})
}